package com.now.printer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.now.printer.R;
import com.now.printer.db.HtmlCollectTable;

import java.util.List;

/**
 * 所有班级适配器，Spinner
 *
 * @ClassName ClassListAdapter
 * @Date: 2020/10/11 17:56
 * @Contact 66010371@163.com
 * @Author: Qingkang.Zhao
 * @Version: v1.0
 * @Description: TODO
 */
public class HtmlCollectAdapter extends BaseAdapter {

    private Context context;
    private List<HtmlCollectTable> classList;
    private LayoutInflater mInflater = null;

    private int mSelectPosition = -1;

    public HtmlCollectAdapter(Context context, List<HtmlCollectTable> classList) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        this.classList = classList;
    }

    @Override
    public int getCount() {
        return classList.size();
    }

    @Override
    public Object getItem(int position) {
        return classList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void changeSelected(int positon) { //刷新方法
        if (positon != mSelectPosition) {
            mSelectPosition = positon;
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_html_collect, null);

            holder.iv_icon = convertView.findViewById(R.id.iv_default);
            holder.tv_name = convertView.findViewById(R.id.tv_name);

            //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        holder.iv_icon.setText(classList.get(position) + "班");
        holder.tv_name.setText(classList.get(position).getHtmlName());
        int imageId = classList.get(position).getHtmlImage();
        if (imageId != 0)
            holder.iv_icon.setImageResource(imageId);
        return convertView;
    }

    private static class ViewHolder {
        private ImageView iv_icon;
        private TextView tv_name;

    }


}